package cn.jdy.plugins.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Productx;
import cn.jdy.plugins.service.ProductxService;

/**
 * daihu
 * @title 产品控制类
 */
@Controller
@RequestMapping("/productx")
public class ProductxCtrl extends BaseCtrl {
  @Autowired
  private ProductxService productxService;
  @Autowired
  private ConfService confService;
/**
 * web版获取商品列表
 * @param productx
 * @param key
 * @param columnid
 * @param pid
 * @return
 */
  @RequestMapping("")
  public ModelAndView list(Productx productx) {
    ModelAndView mv = new ModelAndView("/productx/productxList.jsp");
    try{
      if(productx.getiDisplayLength() == 0) productx.setiDisplayLength(15);                //默认长度5
      if(productx.getPageNo()<=0)  productx.setPageNo(1);
      //默认页数1
      productx.setiDisplayStart((productx.getPageNo() - 1) * productx.getiDisplayLength());  //显示起始索引
      productx.setStatus("0");// 状态 0已发布、1已下架、2审核中、3审核不通过
      //判断是否分类
      if(productx.getBy1()!=null && !",".equals(productx.getBy1())){
    	//判断是否二级分类
        if(productx.getBy1().split(",").length>1){
            productx.setColumnId(Long.valueOf(productx.getBy1().split(",")[1]));
        }
        //获取二级分类的ColumnId
        else{
            productx.setColumnId(Long.valueOf(productx.getBy1().split(",")[0]));
        }
        //获取一级分类的id并且把值赋予ColumnId
        mv.addObject("classlist", confService.findConfValueByPid(Long.valueOf(productx.getBy1().split(",")[0])));
      }
      mv.addObject("dataList", productxService.query(productx));
      mv.addObject("pageNo", productx.getPageNo());
      mv.addObject("size", productx.getiDisplayLength());
      mv.addObject("total", productxService.count(productx));
      mv.addObject("productx", productx);
      mv.addObject("key", productx.getKeyword()); 
    }catch(Exception e){
      BaseLog.e(this.getClass(), "查询异常", e);
    }
    return mv;
  }
  
  
  /**
   * 产品详情
   * @param id
   * @return
   */
  @RequestMapping("/show-{id}.html")
  public ModelAndView queryDetail(@PathVariable Long id) {
    ModelAndView mv = new ModelAndView("/productx/productxdetail.jsp");
    mv.addObject("data", id==null||id==0 ? id : this.productxService.find(id));
    return mv;
  }

}
